//个人填报记录

const app = getApp();
var util = require('../../../utils/util.js');
var date = util.formatDate(new Date()), allperson;
var fz_input_id, callphone, zz_gzry, arrived, zzwhere, fhtwvalue, zzmemo="";

Page({
  data: {
    select_zz: false,
    zmyy_fz_tw_moda:true,
    chuli:true,
    items: [
      { name: 'all', value: '所有人员' ,checked:false},
      { name: 'part', value: '未抵达', checked: true },
    ],
    sex_radio: [
      {checked:false, name: '已抵达', value: '已抵达' },
      { checked: false, name: '未抵达', value: '未抵达' },
    ],
    date: date,
    // list_zyh: true,
    list_mzh: true,
    diaglogt: true,
    
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    // imgalist: ['https://7a6d-zmyy-78bd28-1258642890.tcb.qcloud.la/txl.jpg?sign=bccefd9f27cecdde8f3d7e5fff5eae6e&t=1551966525']
  },
  onLoad: function () {
    this.findOpenid();

    if (!wx.cloud) {
      wx.redirectTo({
        url: '../chooseLib/chooseLib',
      })
      return
    }
  },
  zhuanzhen_twvalue_check: function (e) {
    if (parseFloat(fhtwvalue) > 34 && parseFloat(fhtwvalue) < 41) {
      console.log("输出1：", parseFloat(fhtwvalue))
      if (parseFloat(fhtwvalue) >= 37.3) {
        
            this.setData({
              zmyy_fz_tw_moda: false,             
            })
        this.findOpenid();
      }
      else {
        // console.log("体温正常", parseFloat(twvalue))
        
        this.chuli_up();
        this.setData({
          zmyymz_fz_tw_moda: true,
          items: [
            { name: 'all', value: '所有人员', checked: false },
            { name: 'part', value: '未抵达', checked: true },
          ],
        })
         
      }
    }
    else {
      // console.log("不在范围", parseFloat(twvalue))
      wx.showToast({
        title: '体温不对请重测',
        icon: 'success'
      })
    }
  },
  //确定按钮点击事件,门诊体温
  zmyymz_tw_en: function () {
    // console.log('确定按钮点击事件,门诊体温') 
    this.chuli_up();
    this.setData({
      zmyy_fz_tw_moda: true,
      items: [
        { name: 'all', value: '所有人员', checked: false },
        { name: 'part', value: '未抵达', checked: true },
      ],
    })
  },
  //取消按钮点击事件,门诊体温
  zmyymz_tw_ca: function () {
    // console.log('取消按钮点击事件,门诊体温：') 
    // wx.navigateTo({
    //   url: '/pages/gzryinfo/gzryinfo'
    // })
    this.setData({
      zmyy_fz_tw_moda: true
    })
  },
  chuli_up: function (e) {
    let that =this;
    // console.log("arrived 外", arrived)
    if (arrived ) {
      // console.log("arrived true", arrived)
            if (zzwhere) {
              console.log("zzwhere", zzwhere)
                    if (fhtwvalue) {
                      // console.log("fhtwvalue传入云函数", fz_input_id,fhtwvalue, arrived, zzwhere, zzmemo)
                            wx.cloud.callFunction({
                              name: "update_zmyy_fz",///////////////传参数
                              data: {
                                fz_input_id: fz_input_id,
                                arrived: arrived,
                                zzwhere: zzwhere,
                                fhtwvalue: fhtwvalue,
                                zzmemo: zzmemo,
                              },
                              success(res) {
                                // console.log("读取成功cloud_data", res)
                                that.setData({
                                 chuli:true,
                                 select_zz:false,
                                  sex_radio: [
                                    { checked: false, name: '已抵达', value: '已抵达' },
                                    { checked: false, name: '未抵达', value: '未抵达' },
                                  ],
                                  zzwhere:"",
                                  fhtwvalue:"",
                                  zzmemo:"",
                               })
                                that.findOpenid();
                              },
                              fail(res) {
                                console.log("读取失败getusers", res)
                              }
                            })
                      
                               
                                

                      }
                      else {
                        wx.showToast({
                          title: '填写腋下体温',
                        })
                      }

            }
            else {
              wx.showToast({
                title: '填写转诊去向',
              })
            }

    }
    else {
       wx.showToast({
              title: '选择是否抵达',
            })
    }
  },
  arrived: function (e) {
    arrived=e.detail.value;
    
  },
  zzwhere: function (e) {
    zzwhere = e.detail.value;
    // console.log("zzwhere", zzwhere)
  },
  fhtwvalue: function (e) {
    fhtwvalue = e.detail.value;
    // console.log("fhtwvalue", fhtwvalue)
  },
  zzmemo: function (e) {
    zzmemo = e.detail.value;
    // console.log("zzmemo", zzmemo)
  },
  zhuanzhen_chuli(e) { 
    let arrsex = [] //存放选中id的数组
    let json;
    // console.log('发送选择改变，携带值为', e.currentTarget.dataset.index.arrived)
    try {

   
    if (e.currentTarget.dataset.index.arrived != "") {
        for (let i = 0; i < this.data.sex_radio.length; i++) {
          // console.log('e.currentTarget.dataset.)', e.currentTarget.dataset.index.arrived.includes(this.data.sex_radio[i].name))
          if (e.currentTarget.dataset.index.arrived.includes(this.data.sex_radio[i].name)) {
            // 全选获取选中的值
            json = { "checked": true, "name": this.data.sex_radio[i].name, "value": this.data.sex_radio[i].value }
          }
          else {
            json = { "checked": false, "name": this.data.sex_radio[i].name, "value": this.data.sex_radio[i].value }
          }
          arrsex.push(json);
          // console.log('arrsex', arrsex)
        }
      }
      else {
        for (let i = 0; i < this.data.sex_radio.length; i++) {
          json = { "checked": false, "name": this.data.sex_radio[i].name, "value": this.data.sex_radio[i].value }
          arrsex.push(json);
        }
      }
       this.setData({
        sex_radio: arrsex,
      })
    } catch {

    }
     
    // console.log('arrsex,out', arrsex)
    this.setData({
      select_zz: true,
      chuli: false,
      name: e.currentTarget.dataset.index.name,
      sex: e.currentTarget.dataset.index.sex,
      sfz: e.currentTarget.dataset.index.sfz,
      address: e.currentTarget.dataset.index.address,
      fromwhere: e.currentTarget.dataset.index.fromwhere,
      lxbx: e.currentTarget.dataset.index.fromwhere,
      lxbx04zhengzhuang: e.currentTarget.dataset.index.fromwhere,
      lxbx4other1: e.currentTarget.dataset.index.fromwhere,
      zhengzhuang1: e.currentTarget.dataset.index.fromwhere,
      memo: e.currentTarget.dataset.index.memo,
      twvalue: e.currentTarget.dataset.index.twvalue,
      callphone : e.currentTarget.dataset.index.phone,
      zzwhere: e.currentTarget.dataset.index.zzwhere,
      fhtwvalue: e.currentTarget.dataset.index.fhtwvalue,
      zzmemo: e.currentTarget.dataset.index.zzmemo

    })
    arrived = e.currentTarget.dataset.index.arrived,
      zzwhere= e.currentTarget.dataset.index.zzwhere,
      fhtwvalue= e.currentTarget.dataset.index.fhtwvalue,
      zzmemo= e.currentTarget.dataset.index.zzmemo
    fz_input_id = e.currentTarget.dataset.index._id,
    callphone = e.currentTarget.dataset.index.phone
    
  },
  checkboxChange: function (e) {
    // console.log('发送选择改变，携带值为', e.detail.value)
    allperson = e.detail.value
    if (allperson == "all") {
      this.find_data_mz_all();
      // this.find_data_zy_all();
    } else {
      this.find_data_mz();
      // this.find_data_zy();
    }
  },
  bindDateChange: function (e) {

    this.setData({
      date: e.detail.value
    })
    date = e.detail.value
    if (allperson == "all") {
      this.find_data_mz_all();
      // this.find_data_zy_all();
    } else {
      this.find_data_mz();
      // this.find_data_zy();
    }
  },
  findOpenid: function () {
    const db = wx.cloud.database();

    wx.cloud.callFunction({
      name: 'login',
      complete: res => {
        app.globalData.openid = res.result.openid
        // console.log('onGetOpenid result: ', app.globalData.openid)    
        this.find_data_mz();
        // this.find_data_zy();
      }
    })

  },
  test: function () {

    const db = wx.cloud.database();
    //返回数据记录总数
    wx.cloud.callFunction({
      name: 'record_count',
      data: {
        _openid: "oL22f4qviFt-rMECN4sH59EW-q4s",
        // form: "住院分诊台",
        // gzry_addtime: date
      },
      complete: res => {
        this.setData({
          // list_mz: res.result.data,
          data_count_mz: res.result.total
        })
      }
    })
    //返回数据记录前50条
    wx.cloud.callFunction({
      name: 'record_f100',
      data: {
        _openid: "oL22f4qviFt-rMECN4sH59EW-q4s",
        // form: "住院分诊台",
        // gzry_addtime: date
      },
      complete: res => {
        this.setData({
          list_mz: res.result.data,
        })
      }
    })





  },
  find_data_mz: function () {
    const db = wx.cloud.database();

    //返回数据记录总数
    wx.cloud.callFunction({
      name: 'fenzhen_count',
      data: {
        arrived:"未抵达",
        form: "门诊分诊台",
        gzry_addtime: date
      },
      complete: res => {
        this.setData({
          // list_mz: res.result.data,
          data_count_mz: res.result.total
        })        
      }
    })
    //返回数据记录前50条
    wx.cloud.callFunction({
      name: 'fenzhen_f50',
      data: {
        arrived: "未抵达",
        form: "门诊分诊台",
        gzry_addtime: date
      },
      complete: res => {
        this.setData({
          list_mz: res.result.data,
        })
      }
    })




  },
  
  find_data_mz_all: function () {
    const db = wx.cloud.database();

    //返回数据记录总数
    wx.cloud.callFunction({
      name: 'fenzhen_count',
      data: {
        form: "门诊分诊台",
        gzry_addtime: date
      },
      complete: res => {
        this.setData({
          // list_mz: res.result.data,
          data_count_mz: res.result.total
        })
      }
    })
    //返回数据记录前50条
    wx.cloud.callFunction({
      name: 'fenzhen_f50',
      data: {
        form: "门诊分诊台",
        gzry_addtime: date
      },
      complete: res => {
        this.setData({
          list_mz: res.result.data,
        })
      }
    })
  },
  
  bodadianhua(e) {
    wx.makePhoneCall({
      phoneNumber: callphone
    })
    console.log(callphone)
    console.log(e)
  },


  //确定按钮点击事件
  diag_mz: function () {
    this.setData({
      list_mzh: false,
      // list_zyh: true,
      select_fz: "最近转诊人员(最后50条)",
      fzH: false,
    })

  },
  //取消按钮点击事件
  // diag_zy: function () {
  //   this.setData({
  //     list_mzh: true,
  //     list_zyh: false,
  //     select_fz: "住院分诊台(最后50条)",
  //     fzH: false
  //   })
  // },


	/** 
	 * 预览图片
	 */
  previewImage: function (e) {
    var current = e.target.dataset.src;
    wx.previewImage({
      current: current, // 当前显示图片的http链接
      urls: this.data.imgalist // 需要预览的图片http链接列表
    })
  }


})
